﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class ActionTypeController : BaseDataController
    {
        //
        // GET: /ActionType/

        public ActionResult ActionType()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        ///<summary>
        ///查询日志类型
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string typeName, string startDate, string endDate)
        {
            string where = "  where 1=1 ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            if (typeName != "")
            {
                where += " and a.typeName='" + typeName + "'";
            }
            if (startDate != "" && endDate != "")
            {
                where += " and a.CREATEDATE BETWEEN  to_date('" + startDate + "','yyyy-MM-dd') AND to_date('" + endDate + " 23:59:59 ','yyyy-mm-dd hh24:mi:ss')";
            }

            var retData = new object();
            BAction db = new BAction();
            retData = db.GetActionTypeInfo(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }



        /// <summary>
        /// 修改or新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                ActionType at = new ActionType();

                string projectName = Request["projectName"];
                string stypeName = Request["stypeName"];
                string typeCode = Request["typeCode"];

                at.PROJECTNAME = projectName;
                at.TYPENAME = stypeName;
                at.USERCODE = model.USERCODE;
                at.TYPECODE = Convert.ToInt32(typeCode);
                BAction db = new BAction();


                List<ActionType> list = db.GetActionTypeInfoByCode(at.TYPECODE);
                if (list.Count > 0)
                {
                    sjson = "{success:false,msg:'此日志编号已经存在！'}";
                }
                else
                {
                    int n = db.AddLmActionTypeInfo(at);

                    if (n > 0)
                    {
                        sjson = "{success:true}";
                    }
                    else
                    {
                        sjson = "{success:false,msg:'添加失败.请联系管理员！'}";
                    }
                }

                return sjson;

            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }

    }
}
